/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package converter;

import entities.Comment;
import java.net.URI;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;

/**
 *
 * @author Alex
 */

@XmlRootElement(name = "comment")
public class CommentConverter {
    private Comment entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of CommentConverter */
    public CommentConverter() {
        entity = new Comment();
    }

    /**
     * Creates a new instance of CommentConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public CommentConverter(Comment entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getId() + "/").build() : uri;
        this.expandLevel = expandLevel;
    }

    /**
     * Creates a new instance of CommentConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public CommentConverter(Comment entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for id.
     *
     * @return value for id
     */
    @XmlElement
    public Long getId() {
        return (expandLevel > 0) ? entity.getId() : null;
    }

    /**
     * Setter for id.
     *
     * @param value the value to set
     */
    public void setId(Long value) {
        entity.setId(value);
    }

    /**
     * Getter for text.
     *
     * @return value for text
     */
    @XmlElement
    public String getText() {
        return (expandLevel > 0) ? entity.getText() : null;
    }

    /**
     * Setter for text.
     *
     * @param value the value to set
     */
    public void setText(String value) {
        entity.setText(value);
    }

    /**
     * Getter for addDate.
     *
     * @return value for addDate
     */
    @XmlElement
    public Date getAddDate() {
        return (expandLevel > 0) ? entity.getAddDate() : null;
    }

    /**
     * Setter for addDate.
     *
     * @param value the value to set
     */
    public void setAddDate(Date value) {
        entity.setAddDate(value);
    }

    @XmlElement
    public Long getNoteId() {
        return (expandLevel > 0) ? entity.getIdNote() : null;
    }

    public void setNoteId(Long value) {
        entity.setIdNote(value);
    }

    @XmlElement
    public String getUsername() {
        return (expandLevel > 0) ? entity.getUsername() : null;
    }

    public void setUsername(String value) {
        entity.setUsername(value);
    }

    /**
     * Getter for idUser.
     *
     * @return value for idUser
     */
    @XmlElement
    public Long getIdUser() {
        return (expandLevel > 0) ? entity.getIdUser() : null;
    }

    /**
     * Setter for idUser.
     *
     * @param value the value to set
     */
    public void setIdUser(Long value) {
        entity.setIdUser(value);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the Comment entity.
     *
     * @return an entity
     */
    @XmlTransient
    public Comment getEntity() {
        if (entity.getId() == null) {
            CommentConverter converter = UriResolver.getInstance().resolve(CommentConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved Comment entity.
     *
     * @return an resolved entity
     */
    public Comment resolveEntity(EntityManager em) {
        return entity;
    }
}
